package files;

import java.io.File;
import java.util.Arrays;

import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

/**
 * The methods in this class allow the JTree component to traverse
 * the file system tree, and display the files and directories.
 **/
public class FileTreeModel implements TreeModel {
	// We specify the root directory when we create the model.
	protected File root;
	private String[] types;
	
	public FileTreeModel(File root, String[] types) {
		this.root = root;
		this.types = types;
	}

	// The model knows how to return the root object of the tree
	public Object getRoot() { return new ListFile(root.getPath()); }

	// Tell JTree whether an object in the tree is a leaf or not
	public boolean isLeaf(Object node) {  return ((File)node).isFile(); }
	
	private String[] list(Object parent) {
		File[] dirs_tmp = ((File)parent).listFiles(new DirFilter());
		File[] imgs_tmp = ((File)parent).listFiles(new ImageFilter(types));
		String[] dirs = new String[dirs_tmp.length];
		String[] imgs = new String[imgs_tmp.length];
		
		
		for(int i = 0; i < dirs_tmp.length; i++) {
			dirs[i] = dirs_tmp[i].getName();
		}
		
		for(int j = 0; j < imgs_tmp.length; j++) {
			imgs[j] = imgs_tmp[j].getName();
		}
		
		String[] files = concat(dirs,imgs);
		
		return files;
	}

	// Tell JTree how many children a node has
	public int getChildCount(Object parent) {
		String[] children = list(parent);
		if (children == null) return 0;
		return children.length;
	}

	// Fetch any numbered child of a node for the JTree.
	// Our model returns File objects for all nodes in the tree.  The
	// JTree displays these by calling the File.toString() method.
	public Object getChild(Object parent, int index) {
		String[] children = list(parent);
		if ((children == null) || (index >= children.length)) return null;
		return new ListFile((File) parent, children[index]);
	}

	// Figure out a child's position in its parent node.
	public int getIndexOfChild(Object parent, Object child) {
		String[] children = list(parent);
		if (children == null) return -1;
		String childname = ((File)child).getName();
		for(int i = 0; i < children.length; i++) {
			if (childname.equals(children[i])) return i;
		}
		return -1;
	}

	private String[] concat(String[] first, String[] second) {
		String[] result = Arrays.copyOf(first, first.length + second.length);
		System.arraycopy(second, 0, result, first.length, second.length);
		return result;
	}

	// This method is only invoked by the JTree for editable trees.  
	// This TreeModel does not allow editing, so we do not implement 
	// this method.  The JTree editable property is false by default.
	public void valueForPathChanged(TreePath path, Object newvalue) {}

	// Since this is not an editable tree model, we never fire any events,
	// so we don't actually have to keep track of interested listeners.
	public void addTreeModelListener(TreeModelListener l) {}
	public void removeTreeModelListener(TreeModelListener l) {}
}